package me.stone.stanimeclient.core.ui.adapters.refresh

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.view.ContextThemeWrapper
import androidx.asynclayoutinflater.view.AsyncLayoutInflater

object DefaultAsyncLayoutInflater : PreInflaterHelper.ILayoutInflater {

    private var inflater: AsyncLayoutInflater? = null

    fun get(): DefaultAsyncLayoutInflater {
        return this
    }

    override fun inflate(parent: ViewGroup, layoutId: Int): View {
        return LayoutInflater.from(parent.context).inflate(
            layoutId, parent, false
        )
    }

    override fun asyncInflateView(
        parent: ViewGroup,
        layoutId: Int,
        callback: PreInflaterHelper.InflateCallback
    ) {
        if (inflater == null) {
            val context = parent.context
            inflater = AsyncLayoutInflater(
                ContextThemeWrapper(
                    context.applicationContext, context.theme
                )
            )
        }
        inflater?.inflate(layoutId, parent) { view, id, _ ->
            callback.onInflateFinished(id, view)
        }
    }

}